package com.yiqi.lottery.feature.football.view;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.yiqi.lottery.R;
import com.yiqi.lottery.common.util.DataManager;
import com.yiqi.lottery.entity.bo.BallMatchBo;
import com.yiqi.lottery.entity.bo.LotteryHistoryBo;
import com.yiqi.lottery.entity.bo.LuckyHistoryBo;
import com.yiqi.lottery.feature.football.adapter.FootballSFCSelectedAdapter;
import com.yiqi.lottery.feature.home.view.PayActivity;
import com.yiqi.lottery.feature.login.view.LoginActivity;
import com.yiqi.lottery.feature.lottery.contract.LottoContract;
import com.yiqi.lottery.feature.lottery.dependencies.lotto.DaggerLottoComponent;
import com.yiqi.lottery.feature.lottery.dependencies.lotto.LottoPresenterModule;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

public class FootballSFCCalculateMoneyActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener, LottoContract.View {

    @Inject
    LottoContract.Presenter mPresenter;
    @BindView(R.id.tv_back)
    TextView tv_back;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_hemai)
    TextView tv_hemai;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_award)
    TextView tv_award;
    @BindView(R.id.tv_buy)
    TextView tv_buy;
    @BindView(R.id.ll_jian)
    View ll_jian;
    @BindView(R.id.tv_beishu)
    EditText tv_beishu;
    @BindView(R.id.ll_jia)
    View ll_jia;
    @BindView(R.id.tvSelectWay)
    TextView tvSelectWay;

    FootballSFCSelectedAdapter mAdapter;
    int mCurrentBeishu;

    private BallMatchBo mSelectedMatchs;
    private int mCurrentBetType;
    private int mTotalMoney;

    int mCurrentZhushu;

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        return false;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_footall_sfc_calculate_money;
    }

    @Override
    protected void initView() {
        mSelectedMatchs = (BallMatchBo) DataManager.getInstance().getTempObject();
        DataManager.getInstance().setTempObject(null);
        mCurrentBetType = getIntent().getIntExtra("current_type", 0);
        tvSelectWay.setText(mCurrentBetType == 9 ? "胜负彩" : "任选九");
        mCurrentBeishu = 1;
        tv_beishu.setText(String.valueOf(mCurrentBeishu));
        //  resetMoney();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        getAdapter().bindToRecyclerView(recyclerView);
        getAdapter().setMatchBo(mSelectedMatchs);
        getAdapter().setNewData(mSelectedMatchs.getMatch());
    }

    public void onResume() {
        super.onResume();
        getAdapter().notifyDataSetChanged();
    }


    private FootballSFCSelectedAdapter getAdapter() {
        if (mAdapter == null) {
            mAdapter = new FootballSFCSelectedAdapter(this, mCurrentBetType);
            mAdapter.setOnMatchSelectListener(new FootballSFCSelectedAdapter.OnMatchSelectListener() {
                @Override
                public void reset() {
                    resetMoney();
                }
            });
        }
        return mAdapter;
    }

    private void resetMoney() {
        mCurrentZhushu = 0;
        List<BallMatchBo.Match> selectedMatchs = getCheckedMatches();
        int selectedMatchNum = selectedMatchs.size();
        int minNeedMatchNum = (mCurrentBetType == 9 ? 14 : 9);
        int tempBeishu = 0;
        if (selectedMatchNum >= minNeedMatchNum) {
            mCurrentZhushu += getCount(0, selectedMatchNum, minNeedMatchNum, selectedMatchs);
            tempBeishu = mCurrentBeishu;
        }
        mTotalMoney = mCurrentZhushu * tempBeishu * 2;
        tv_money.setText(Html.fromHtml("共<font color=\"#D83220\">" + mCurrentZhushu + "</font>注 "
                + "<font color=\"#D83220\">" + tempBeishu + "</font>倍 "
                + "<font color=\"#D83220\">" + mTotalMoney + "</font>元"));
    }

    private int getCount(int index, int totalMatchNum, int num, List<BallMatchBo.Match> list) {
        if (num == 0) {
            return 1;
        }
        if (index == totalMatchNum) {
            return 0;
        }
        int count = 0;
        int tempCount = 0;
        int checkNum;
        for (int i = index; i < totalMatchNum; ++i) {
            checkNum = list.get(i).getCheckedNum();
            tempCount = getCount(i + 1, totalMatchNum, num - 1, list);
            if (tempCount > 0) {
                count += checkNum * tempCount;
            }
        }
        return count;
    }

    private List<BallMatchBo.Match> getCheckedMatches() {
        List<BallMatchBo.Match> selectedMatchs = new ArrayList<>();
        if (mSelectedMatchs.getMatch() != null) {
            for (BallMatchBo.Match match : mSelectedMatchs.getMatch()) {
                if (match.isHadChecked()) {
                    selectedMatchs.add(match);
                }
            }
        }
        return selectedMatchs;
    }

    @Override
    protected void initInjector() {
        DaggerLottoComponent.builder().lottoPresenterModule(new LottoPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tv_back, R.id.ll_jian, R.id.ll_jia, R.id.tv_buy, R.id.tv_hemai})
    public void onViewClicked(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_back:
                finish();
                break;
            case R.id.ll_jian:
                if (mCurrentBeishu > 1) {
                    tv_beishu.setText(String.valueOf(--mCurrentBeishu));
                    resetMoney();
                }
                break;
            case R.id.ll_jia:
                tv_beishu.setText(String.valueOf(++mCurrentBeishu));
                resetMoney();
                break;
            case R.id.tv_hemai:
            case R.id.tv_buy:
                if (!AM.user().isLogin()) {
                    startActivity(new Intent(this, LoginActivity.class));
                    return;
                }
                if (mCurrentZhushu == 0) {
                    T.showShort("请至少选择一注！");
                    return;
                }

                int type = 2;
                switch (mCurrentBetType) {
                    case 9://胜负彩
                        type = 2;
                        break;
                    case 10://任选9
                        type = 3;
                        break;
                }
                if(id==R.id.tv_hemai) {
                    Intent intent = new Intent(this, FootballMatchHemaiActivity.class);
                    intent.putExtra("total_money", mTotalMoney);
                    intent.putExtra("bet_type", type);
                    intent.putExtra("bet_code",getBetCode());
                    intent.putExtra("multiple", mCurrentBeishu);
                    intent.putExtra("issue",getAdapter().getMatchBo().getIssue());
                    intent.putExtra("finish_time",getAdapter().getMatchBo().getStopTime());
                    startActivity(intent);
                }else {
                    Intent intent = new Intent(this, PayActivity.class);
                    intent.putExtra("order_type", type);
                    intent.putExtra("data", getBetCode());
                    intent.putExtra("multiple", mCurrentBeishu);
                    intent.putExtra("mut_num", mCurrentZhushu);
                    intent.putExtra("issue", getAdapter().getMatchBo().getIssue());
                    intent.putExtra("total_amount", 2 * mCurrentBeishu * mCurrentZhushu);
                    intent.putExtra("is_charsing", 2);
                    intent.putExtra("charsing_num", 1);
                    startActivity(intent);
                    overridePendingTransition(R.anim.push_bottom_in, R.anim.push_buttom_out);
           //         mPresenter.doCreateOrder(type, mCurrentBeishu, getBetCode(), getAdapter().getMatchBo().getIssue(),2,0,mCurrentZhushu);
                }
                break;
        }
    }

    private String getBetCode() {

        StringBuilder bet_code = new StringBuilder();
        List<BallMatchBo.Match> list = getAdapter().getData();
        switch (mCurrentBetType) {
            case 9:
                for (BallMatchBo.Match match : list) {
                    if (match.isCheck(0)) {
                        bet_code.append("3");
                    }
                    if (match.isCheck(1)) {
                        bet_code.append("1");
                    }
                    if (match.isCheck(2)) {
                        bet_code.append("0");
                    }
                    bet_code.append(",");
                }
                bet_code.deleteCharAt(bet_code.length() - 1);
                break;
            case 10:
                int index = 1;
                for (BallMatchBo.Match match : list) {
                    bet_code.append(String.valueOf(index));
                    bet_code.append(":");
                    if (match.isHadChecked()) {
                        if (match.isCheck(0)) {
                            bet_code.append("3");
                        }
                        if (match.isCheck(1)) {
                            bet_code.append("1");
                        }
                        if (match.isCheck(2)) {
                            bet_code.append("0");
                        }
                        bet_code.append(match.isDan() ? "`1" : "`0");
                    } else {
                        bet_code.append("#`#");
                    }
                    bet_code.append(",");
                    ++index;
                }
                bet_code.deleteCharAt(bet_code.length() - 1);
                break;
        }
        return bet_code.toString();
    }

    @Override
    public void showCurrentSuccess(LuckyHistoryBo luckyHistoryBo) {

    }

    @Override
    public void showCurrentError(String ex) {

    }

    @Override
    public void showCreateOrderSuccess(String luckyHistoryBo) {
        T.showShort("下单成功！");
        finish();
    }

    @Override
    public void showCreateOrderError(String ex) {

    }

    @Override
    public void showLotteryHistorySuccess(List<LotteryHistoryBo> resultBos) {

    }

    @Override
    public void showLotteryHistoryError(String error) {

    }
}
